#include <readData.h>

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>
#include <fstream>
#include <assert.h>

#include<TCanvas.h>
#include<TH1F.h>
#include<TH2F.h>
#include<TF1.h>
#include<TStyle.h>
#include<TRandom.h>

readData::readData(Int_t k){
	//Int_t privet = k;

}
void readData::graph_plot(){
	TCanvas *c2 = new TCanvas("c2", "c2", 600, 400);
	TH2F *hlego2 = new TH2F("hlego2", "Option LEGO2Z example ", 100, -4, 4, 100, 20, 20);
	Float_t px, py;
	for (Int_t i = 0; i < 25000; i++) {
		gRandom->Rannor(px, py);
		hlego2->Fill(px - 1, 5 * py);
		hlego2->Fill(2 + 0.5 * px, 2 * py - 10., 0.1);
	}
	gStyle->SetPalette(1);
	hlego2->Draw("LEGO2Z");
	//return c2;
}
void readData::openFile(Int_t NumOpenFile){
	sprintf(Path, "./../data/data%d.raw", NumOpenFile);
	SiFile = fopen("data16.raw", "rb");

	if (!SiFile) {
		cout << "!!! ERROR !!!" << endl;
		std::cout << "File " << Path << " does not exist" << std::endl;
	}
	if (SiFile) {
		printf("----===== File is open =====----- \n");
	}
}
void readData::closeFile(){
	fclose(SiFile);
}
void readData::readFile(){
	int tmp;
	Int_t tmp_look[2];
	//Int_t i=0;
	Int_t NumOfDetector;
	Int_t tsikl = 0;
	Int_t count =0;

	tmp_look[0] = 0;
	tmp_look[1] = 0;

	Int_t Counter0;
	Int_t Counter1;
	Int_t DataLength0;
	Int_t DataLength1;
	Int_t StatusLine;
	Int_t OVF;
	Int_t NumOfState;
	Int_t State;
	Int_t AddColum;
	Int_t AddLine;
	Int_t HitPix;
	Int_t Trailer0;
	Int_t Trailer1;
	Int_t DataLength;
	Int_t NumOfLengt;


	TCanvas *c2 = new TCanvas("c2", "c2", 1200, 800);
	c2->Divide(3, 2);
	TH2F *hlego2[7];
	hlego2[1]= new TH2F("=1=", "chip# 11", 550, 0, 1152, 270, 0, 576);
	hlego2[2]= new TH2F("=2=", "chip# 12", 550, 0, 1152, 270, 0, 576);
	hlego2[3]= new TH2F("=3=", "chip# 19", 550, 0, 1152, 270, 0, 576);
	hlego2[4]= new TH2F("=4=", "chip# 4", 550, 0, 1152, 270, 0, 576);
	hlego2[5]= new TH2F("=5=", "chip# 2", 550, 0, 1152, 270, 0, 576);
	hlego2[6]= new TH2F("=6=", "chip# 20", 550, 0, 1152, 270, 0, 576);
	/*
	Float_t px, py;
	for (Int_t i = 0; i < 25000; i++) {
		gRandom->Rannor(px, py);
		hlego2->Fill(px - 1, 5 * py);
		hlego2->Fill(2 + 0.5 * px, 2 * py - 10., 0.1);
	}
	gStyle->SetPalette(1);
	hlego2->Draw("LEGO2Z");
	//return c2;
	*/


	while (tsikl ==0 ) {
		count ++;
		//printf("count= %d \n", count);
		if (feof(SiFile) || ferror(SiFile))	{cout << "=1=Error or END of file" << endl; break; }
		tmp_look[1] = 0;
		tmp_look[0] = 0;
		while (1) {
			if (feof(SiFile) || ferror(SiFile))	{cout << "=2=Error or END of file" << endl; break; }
			fread(&tmp, 2, 1, SiFile);
			if (tmp == EOF ) printf("=2= EOF \n");
			tmp_look[0] = tmp;
			//printf("tmp= 0x%x \n", tmp);
			if ( (tmp_look[0] == 0x5551) & (tmp_look[1] == 0x5555) ) { NumOfDetector =1; break; }
			if ( (tmp_look[0] == 0x5552) & (tmp_look[1] == 0x5555) ) { NumOfDetector =2; break; }
			if ( (tmp_look[0] == 0x5553) & (tmp_look[1] == 0x5555) ) { NumOfDetector =3; break; }
			if ( (tmp_look[0] == 0x5554) & (tmp_look[1] == 0x5555) ) { NumOfDetector =4; break; }
			if ( (tmp_look[0] == 0x5555) & (tmp_look[1] == 0x5555) ) { NumOfDetector =5; break; }
			if ( (tmp_look[0] == 0x5556) & (tmp_look[1] == 0x5555) ) { NumOfDetector =6; break; }
			tmp_look[1] = tmp_look[0];
			tmp_look[0] = 0;
			tmp=0;
		}
		//Read Counter
		for (int k = 0; k < 2; k++) {
			if (feof(SiFile) || ferror(SiFile))	{cout << "=3=Error or END of file" << endl; break; }
			fread(&tmp, 2, 1, SiFile);
			//printf("tmp= 0x%x \n", tmp);
			if (tmp == EOF ) printf("=3= EOF \n");
			if (k == 0)		Counter0 = tmp;
			if (k == 1)		Counter1 = tmp;
			tmp=0;
		}
		//printf("NumOfDetector=%d \n", NumOfDetector);
		//printf("Counter1=%d", Counter1);
		//printf(" = 0x%x", Counter1);
		//printf("           Counter0=%d", Counter0);
		//printf(" = 0x%x\n ", Counter0);

		//Read data length
		for (int k = 0; k < 2; k++) {
			if (feof(SiFile) || ferror(SiFile))	{cout << "=4=Error or END of file" << endl; break; }
			fread(&tmp, 2, 1, SiFile);
			//printf("tmp= 0x%x \n", tmp);
			if (tmp == EOF ) printf("=4= EOF \n");
			if (k == 0)	DataLength0 = tmp;
			if (k == 1)	DataLength1 = tmp;
			tmp=0;
		}
		DataLength = DataLength0 + DataLength1;
		//printf("DataLength0=%d", DataLength0);
		//printf(" = 0x%x ", DataLength0);
		//printf("          DataLength1=%d", DataLength1);
		//printf(" = 0x%x \n", DataLength1);

		//Read Status
		NumOfLengt = 0;
		while (1) {
			if (NumOfLengt == DataLength) break;
			if (feof(SiFile) || ferror(SiFile))	{cout << "=5=Error or END of file" << endl; break; }
			NumOfLengt++;
			fread(&tmp, 2, 1, SiFile);
			//printf("tmp= 0x%x \n", tmp);
			if (tmp == EOF ) printf("=5= EOF \n");
			//printf("tmp=%d ", tmp);
			StatusLine = tmp;
			OVF = StatusLine >> 15;
			NumOfState = StatusLine & 0xF;
			AddLine = (StatusLine >> 4) & 0x7FF;
			tmp=0;
			//printf("OVF=%d ", OVF);
			//printf("AddLine=%d ", AddLine);
			//printf("NumOfState=%d ", NumOfState);
			//printf("                             tmp=0x%x \n", tmp);

			//Read State
			for (int k = 0; k < NumOfState; k++) {
				if (NumOfLengt == DataLength) break;
				NumOfLengt++;
				if (feof(SiFile) || ferror(SiFile))	{cout << "=6=Error or END of file" << endl; break; }
				fread(&tmp, 2, 1, SiFile);
				//printf("tmp= 0x%x \n", tmp);
				State = tmp;
				AddColum = (State >> 2) & 0x7FF;
				HitPix = State & 0x3;
				//printf("                              AddColum=%d ", AddColum);
				//printf("HitPix=%d ", HitPix);
				//printf("        tmp=0x%x \n", tmp);
				tmp=0;
				for (Int_t i=0; i<=HitPix; i++){
					hlego2[NumOfDetector]->Fill(AddColum, AddLine+i);
				}
			}
		}
		//if (!eof(SiFile) ) printf("=7= EOF \n");
		if (feof(SiFile) || ferror(SiFile))	{cout << "=7=Error or END of file" << endl; break; }
		fread(&tmp, 2, 1, SiFile);
		//printf("tmp= 0x%x \n", tmp);
		//printf("tmp= 0x%x \n", tmp);
		Trailer0 = tmp;
		tmp=0;
		fread(&tmp, 2, 1, SiFile);
		//printf("tmp= 0x%x \n", tmp);
		//printf("tmp= 0x%x \n", tmp);
		Trailer1 = tmp;
		tmp=0;
		//printf("Trailer0=%d \n", Trailer0);
		//printf("Trailer1=%d \n", Trailer1);
	}
	gStyle->SetPalette(1);
	for (Int_t n =1; n<7; n++){
		c2->cd(n);
		hlego2[n]->Draw("COLZ");
	}

	//hlego2->Draw("LEGO2Z");
}
readData::~readData(){
}
